/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JFreeChartExtensions.Collections;


import JFreeChartExtensions.Collections.utils.DatasetSeriesProperties;
import JFreeChartExtensions.renderers.XYErrorRendererExtended;
import JFreeChartExtensions.utils.ColorEnumerator;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.jfree.chart.plot.XYPlot;

/**
 *
 * @author SG0212059
 */
public class DataRenderersCollection implements Cloneable {

    private TreeMap<Integer, XYErrorRendererExtended> renderCollections = new TreeMap<Integer, XYErrorRendererExtended>();
    private DatasetCollection parentCollection;

    public void setParentCollection(DatasetCollection parentCollection) {
        this.parentCollection = parentCollection;
    }
    private int size = 0;
    private XYPlot xyplot;

    public void addRendererForSeries(DatasetSeriesProperties pair) {
        int dataset = pair.dataset;
        if (!renderCollections.containsKey(dataset)) {
            configureRendererForNonRegressionSeries(pair);
        } else {
            configureRendererForRegressionSeries(pair);
        }
    }

    public DataRenderersCollection(DatasetCollection parentCollection) {
        this.parentCollection = parentCollection;
    }

    private void configureRendererForRegressionSeries(DatasetSeriesProperties pair) {
        int dataset = pair.dataset;
        int series = pair.series;
        ((XYErrorRendererExtended) xyplot.getRenderer(dataset)).setSeriesLinesVisible(series, true);
        ((XYErrorRendererExtended) xyplot.getRenderer(dataset)).setSeriesShapesVisible(series, false);
        ((XYErrorRendererExtended) xyplot.getRenderer(dataset)).setSeriesYError(series, false);
        ((XYErrorRendererExtended) xyplot.getRenderer(dataset)).setSeriesXError(series, false);
        if (pair.paint == null) {
            pair.paint = xyplot.getRenderer(dataset).getSeriesPaint(pair.series);
        } else {
            xyplot.getRenderer(dataset).setSeriesPaint(pair.series, pair.paint);

        }
    }

    private void configureRendererForNonRegressionSeries(DatasetSeriesProperties pair) {
        XYErrorRendererExtended errorRenderer = new XYErrorRendererExtended();
        errorRenderer.setSeriesPaint(pair.series, ColorEnumerator.getNextColor());
        xyplot.setRenderer(pair.dataset, errorRenderer);
        renderCollections.put(pair.dataset, errorRenderer);
        if (pair.paint == null) {
            pair.paint = xyplot.getRenderer(pair.dataset).getSeriesPaint(pair.series);
        } else {
            xyplot.getRenderer(pair.dataset).setSeriesPaint(pair.series, pair.paint);

        }
        ++size;

    }

    public TreeMap<Integer, XYErrorRendererExtended> getRenderersCollections() {
        return renderCollections;
    }

    @Override
    public Object clone() {
        DataRenderersCollection datasetCollection = new DataRenderersCollection();
        datasetCollection.addAll(this.getRenderersCollections());
        return datasetCollection;
    }


    public void addAll(TreeMap<Integer, XYErrorRendererExtended> collection) {
        size = collection.size();
        for (Entry<Integer, XYErrorRendererExtended> entry : collection.entrySet()) {
            renderCollections.put(new Integer(entry.getKey()), (XYErrorRendererExtended) entry.getValue());
        }

    }

    public DataRenderersCollection() {
    }

    

    public void setXyplot(XYPlot xyplot) {
        this.xyplot = xyplot;
    }

    public XYPlot getXyplot() {
        return xyplot;
    }

    public void collectionStructureChanged() {
        for (Entry<Integer, XYErrorRendererExtended> entry : renderCollections.entrySet()) {
            xyplot.setRenderer(entry.getKey(), entry.getValue());
        }
    }

    public void revert(){
        for(int i = 0 ; i < xyplot.getRendererCount() ; ++i){
           renderCollections.put(i, (XYErrorRendererExtended) xyplot.getRenderer(i));
        }
    }


    public void setVisibleForAllRegressionSeries(boolean show) {
        for (DatasetSeriesProperties seriesPair : parentCollection.getCordinatesOfAllRegressionSeries()) {
            XYErrorRendererExtended renderer = (XYErrorRendererExtended) xyplot.getRenderer(seriesPair.dataset);
            renderer.setSeriesVisible(seriesPair.series, show);
        }
    }

}
